package com.piotrs10.bsk.gui.views;

import java.util.ArrayList;
import java.util.Iterator;

import com.piotrs10.bsk.dao.PracownikDao;
import com.piotrs10.bsk.dao.SalonDao;
import com.piotrs10.bsk.model.Pracownik;
import com.piotrs10.bsk.model.Salon;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;

public class ModifyPracownik extends ModifyLayout {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8666588207059670132L;

	private TextField imieField, nazwiskoField, pensjaField;
	private ComboBox salonBox;

	public ModifyPracownik(boolean isInsert, Integer id, Window modalWindow) {
		super(isInsert, modalWindow);

		this.imieField = new TextField("Imie");
		this.nazwiskoField = new TextField("Nazwisko");
		this.pensjaField = new TextField("Pensja");
		this.salonBox = new ComboBox("Salon");

		this.loadSalon(isInsert, id);

		this.fieldsPanel.addComponent(this.imieField);
		this.fieldsPanel.addComponent(this.nazwiskoField);
		this.fieldsPanel.addComponent(this.pensjaField);
		this.fieldsPanel.addComponent(this.salonBox);
		this.fieldsPanel.addComponent(this.commitButton);
	}

	private void loadSalon(boolean insert, Integer id) {
		// TODO: Pobrac z bazy danych co trzeba
		ArrayList<Salon> list = new SalonDao().getAll();
		for (Salon current : list) {
			this.salonBox.addItem(current.getId());
		}
		
		if(insert == false) {
			Pracownik p = new PracownikDao().get(id);
			this.imieField.setValue(p.getImie());
			this.nazwiskoField.setValue(p.getNazwisko());
			this.pensjaField.setValue(p.getPensja());

			Iterator<?> iterator = this.salonBox.getItemIds().iterator();
			Object item = null;
			int typId = 0;
			do {
				item = iterator.next();
				typId = Integer.valueOf(this.salonBox.getItemCaption(item));
			} while(typId != p.getSalon());
			this.salonBox.setValue(item);
		}
	}

	@Override
	public void buttonClick(ClickEvent event) {
		// TODO Auto-generated method stub

	}

	public static void fillTable(Table table) {
		table.addContainerProperty("ID", Integer.class, null);
		table.addContainerProperty("Imie", String.class, null);
		table.addContainerProperty("Nazwisko", String.class, null);
		table.addContainerProperty("Pensja", Float.class, null);
		table.addContainerProperty("Salon", Integer.class, null);
		ArrayList<Pracownik> list = new PracownikDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Pracownik p = list.get(j);
			table.addItem(new Object[] { p.getId(), p.getImie(), p.getNazwisko(), 
					p.getPensja(), p.getSalon()}, j);
		}
	}

	public String getWinWidth() {
		return "250px";
	}

	public String getWinHeight() {
		return "260px";
	}
	
	public static void refresh(Table table) {
		table.removeAllItems();
		ArrayList<Pracownik> list = new PracownikDao().getAll();
		for (int j = 0; j < list.size(); j++) {
			Pracownik p = list.get(j);
			table.addItem(new Object[] { p.getId(), p.getImie(), p.getNazwisko(), 
					p.getPensja(), p.getSalon()}, j);
		}
	}

}
